package employee_evaluation

import (
	"goskeleton/app/global/consts"
	"goskeleton/app/service/employee_evaluation/curd"
	"goskeleton/app/utils/cur_userinfo"
	"goskeleton/app/utils/response"
	"time"

	"github.com/gin-gonic/gin"
)

type OvertimeLeaveController struct {
}

// Insert 创建请假申请
func (o *OvertimeLeaveController) Insert(c *gin.Context) {
	employeeId, exist := cur_userinfo.GetCurrentUserEmployeeId(c)
	if !exist {
		response.Fail(c, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
		return
	}
	leaveCurd := curd.CreateLeaveCurdFactory()
	success, msg := leaveCurd.InsertData(c, employeeId)
	if success {
		response.Success(c, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(c, consts.CurdCreatFailCode, msg,"")
	}
}

// Update 更新请假申请
func (o *OvertimeLeaveController) Update(c *gin.Context) {
	leaveCurd := curd.CreateLeaveCurdFactory()
	if leaveCurd.UpdateData(c) {
		response.Success(c, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(c, consts.CurdUpdateFailCode, consts.CurdUpdateFailMsg, "")
	}
}

// List 获取请假申请列表
func (o *OvertimeLeaveController) ListApprovals(context *gin.Context) {
	// 获取查询参数
	page := int(context.GetFloat64(consts.ValidatorPrefix + "page"))
	limit := int(context.GetFloat64(consts.ValidatorPrefix + "limit"))
	order := context.GetString(consts.ValidatorPrefix + "order")

	employeeID := int(context.GetFloat64(consts.ValidatorPrefix + "employee_id"))
	leaveType := context.GetString(consts.ValidatorPrefix + "leave_type")
	departmentId := int(context.GetFloat64(consts.ValidatorPrefix + "department_id"))
	searchDate := context.GetString(consts.ValidatorPrefix + "search_date")

	var startDate, endDate string

	// 处理日期查询 后期优化
	if searchDate != "" {
		t, err := time.Parse("2006-01", searchDate)
		if err != nil {
			response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
			return
		}
		year := t.Year()
		month := t.Month()
		// 创建当月的第一天
		firstDay := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
		// 创建下月的第一天，然后减一天得到当月的最后一天
		lastDay := firstDay.AddDate(0, 1, -1)
		// 设置最后一天的时间为23:59:59
		lastDayAtTime := time.Date(year, month, lastDay.Day(), 23, 59, 59, 0, time.Local)
		startDate = firstDay.Format("2006-01-02 15:04:05")
		endDate = lastDayAtTime.Format("2006-01-02 15:04:05")
	}

	recordType := context.GetString(consts.ValidatorPrefix + "record_type")
	status := context.GetString(consts.ValidatorPrefix + "status")

	leaveCurd := curd.CreateLeaveCurdFactory()
	total, list, totalRecord := leaveCurd.ListData(
		departmentId,
		recordType,
		(page-1)*limit,
		limit,
		order,
		employeeID,
		leaveType,
		startDate,
		endDate,
		status,
		true,
	)

	response.Success(context, "获取列表成功", map[string]interface{}{
		"total":       total,
		"list":        list,
		"totalRecord": totalRecord,
	})
}

func (o *OvertimeLeaveController) ListAttendanceRecords(c *gin.Context) {
	employeeId, exist := cur_userinfo.GetCurrentUserEmployeeId(c)
	if !exist {
		response.Fail(c, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
		return
	}
	// 获取查询参数
	page := int(c.GetFloat64(consts.ValidatorPrefix + "page"))
	limit := int(c.GetFloat64(consts.ValidatorPrefix + "limit"))
	// order := c.GetString(consts.ValidatorPrefix + "order")
	order := "apply_time desc"

	recordType := c.GetString(consts.ValidatorPrefix + "type")
	searchDate := c.GetString(consts.ValidatorPrefix + "search_date")

	var startDate, endDate string

	// 处理日期查询 后期优化
	if searchDate != "" {
		t, err := time.Parse("2006-01", searchDate)
		if err != nil {
			response.Fail(c, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
			return
		}
		year := t.Year()
		month := t.Month()
		// 创建当月的第一天
		firstDay := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
		// 创建下月的第一天，然后减一天得到当月的最后一天
		lastDay := firstDay.AddDate(0, 1, -1)
		// 设置最后一天的时间为23:59:59
		lastDayAtTime := time.Date(year, month, lastDay.Day(), 23, 59, 59, 0, time.Local)
		startDate = firstDay.Format("2006-01-02 15:04:05")
		endDate = lastDayAtTime.Format("2006-01-02 15:04:05")
	}
	// recordType := c.GetString(consts.ValidatorPrefix + "record_type")
	status := c.GetString(consts.ValidatorPrefix + "status")
	var departmentId  int =  0
	leaveCurd := curd.CreateLeaveCurdFactory()
	total, list, totalRecord := leaveCurd.ListData(
		departmentId,
		recordType,
		(page-1)*limit,
		limit,
		order,
		employeeId,
		"",
		startDate,
		endDate,
		status,
		false,
	)

	response.Success(c, "获取列表成功", map[string]interface{}{
		"total":       total,
		"list":        list,
		"totalRecord": totalRecord,
	})
}

// Show 获取请假申请详情
func (o *OvertimeLeaveController) Show(c *gin.Context) {
	id := int(c.GetFloat64(consts.ValidatorPrefix + "id"))
	leaveCurd := curd.CreateLeaveCurdFactory()
	data := leaveCurd.ShowOneItem(id)
	if data != nil {
		response.Success(c, consts.CurdStatusOkMsg, data)
	} else {
		response.Fail(c, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	}
}

// Delete 删除请假申请
func (o *OvertimeLeaveController) Delete(c *gin.Context) {
	id := int(c.GetFloat64(consts.ValidatorPrefix + "id"))
	leaveCurd := curd.CreateLeaveCurdFactory()
	if leaveCurd.DeleteData(id) {
		response.Success(c, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(c, consts.CurdDeleteFailCode, consts.CurdDeleteFailMsg, "")
	}
}

func (o *OvertimeLeaveController) ChangeStatus(c *gin.Context) {
	id := int(c.GetFloat64(consts.ValidatorPrefix + "id"))
	status := c.GetString(consts.ValidatorPrefix + "status")
	rejectionNote := c.GetString(consts.ValidatorPrefix + "rejection_note")

	leaveCurd := curd.CreateLeaveCurdFactory()
	if leaveCurd.ChangeStatus(id, status, rejectionNote) {
		response.Success(c, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(c, consts.CurdUpdateFailCode, consts.CurdUpdateFailMsg, "")
	}
}

func (o *OvertimeLeaveController) GetAllDataByMonth(c *gin.Context) {

	employeeId, exist := cur_userinfo.GetCurrentUserEmployeeId(c)
	if !exist {
		response.Fail(c, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
		return
	}
	searchDate := c.GetString(consts.ValidatorPrefix + "search_date")

	var startDate, endDate string

	// 处理日期查询 后期优化
	if searchDate != "" {
		t, err := time.Parse("2006-01", searchDate)
		if err != nil {
			response.Fail(c, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
			return
		}
		year := t.Year()
		month := t.Month()
		// 创建当月的第一天
		firstDay := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
		// 创建下月的第一天，然后减一天得到当月的最后一天
		lastDay := firstDay.AddDate(0, 1, -1)
		// 设置最后一天的时间为23:59:59
		lastDayAtTime := time.Date(year, month, lastDay.Day(), 23, 59, 59, 0, time.Local)
		startDate = firstDay.Format("2006-01-02 15:04:05")
		endDate = lastDayAtTime.Format("2006-01-02 15:04:05")
	}
	leaveCurd := curd.CreateLeaveCurdFactory()
	totalCounts, tmp, total := leaveCurd.ListAllDataByMonth("", employeeId, startDate, endDate, "")
	if totalCounts > 0 && tmp != nil {
		response.Success(c, consts.CurdStatusOkMsg, map[string]interface{}{
			"total": total,
			"list":  tmp,
		})
	} else {
		response.Fail(c, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	}
}

